#include "StdAfx.h"
#include "Action.h"

Action::Action(ImgResource ^_res,int type,System::Windows::Forms::ContextMenuStrip ^menuRB)
{
	this->actionImg = gcnew System::Windows::Forms::PictureBox();
	this->actionImg->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
	this->actionImg->ContextMenuStrip = menuRB;
	this->actionImg->Location = System::Drawing::Point(552, 392);
	this->actionImg->Size = System::Drawing::Size(0, 0);
	this->actionImg->TabStop = false;
	//this->actionImg->MouseLeave += gcnew System::EventHandler(this, mouseOff);
	//this->actionImg->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, mouseDrag);
	//this->actionImg->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, mouseStartDrag);
	//this->actionImg->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, mouseOverDrag);
	//this->actionImg->MouseEnter += gcnew System::EventHandler(this, mouseOn);

	this->label = gcnew System::Windows::Forms::Label();
	this->label->AutoSize = true;
	this->label->ContextMenuStrip = menuRB;
	this->label->BackColor = System::Drawing::Color::Transparent;
	this->label->ForeColor = System::Drawing::Color::Black;
	this->label->Location = System::Drawing::Point(552, 392);
	this->label->Size = System::Drawing::Size(0, 0);
	this->label->Text = L"label";
	//this->label->DoubleClick += gcnew System::EventHandler(this, MouseDbl);

	this->res = _res;
	set(this->res,type,552,392);
}

Action^ Action::MakeEqual(Action^ other)
{
	this->type = other->type;
	set(other->res,type,other->get('x'),other->get('y'));
	this->actionImg->Size = System::Drawing::Size(other->get('w'), other->get('h'));
	this->label->Text = other->label->Text;
	return this;
}
void Action::paint()
{
	System::Drawing::Point *a;
	if (type == 7)
	{
		a=new System::Drawing::Point(this->actionImg->Location.X+this->actionImg->Size.Width/2+20,this->actionImg->Location.Y+this->actionImg->Size.Height/2-this->label->Size.Height/2);
	}
	else if (type == 4)
	{
		a=new System::Drawing::Point(this->actionImg->Location.X+this->actionImg->Size.Width/2+20,this->actionImg->Location.Y+30);
	}
	else
	{
		a=new System::Drawing::Point(this->actionImg->Location.X+this->actionImg->Size.Width/2-this->label->Size.Width/2,this->actionImg->Location.Y+this->actionImg->Size.Height/2-this->label->Size.Height/2);
	}
	this->label->Location = *(a);
	delete a;
}
void Action::null()
{
	this->label->Visible = false;
	this->actionImg->Visible = false;
	this->actionImg->Location = System::Drawing::Point(0, 0);
	this->actionImg->Size = System::Drawing::Size(0, 0);
	this->label->Location = System::Drawing::Point(0, 0);
	this->label->Size = System::Drawing::Size(0, 0);
	this->type = -1;
}
void Action::set(ImgResource ^res,int type,int x,int y)
{
	System::Drawing::Size *b;
	this->actionImg->Location = System::Drawing::Point(x, y);
	this->label->Location = System::Drawing::Point(x, y);
	this->type = type;
	this->actionImg->BringToFront();
	this->label->BringToFront();
	this->actionImg->Visible = true;
	switch( type )
	{   
		case -1:
			this->actionImg->BackgroundImage = res->action;
			this->label->Visible = false;
			break;
		case 1:
			this->actionImg->BackgroundImage = res->start;
			b=new System::Drawing::Size(30,30);
			this->label->Text = gcnew System::String("Start");
			this->label->Visible = false;
			this->actionImg->Size = *b;
			delete b;
			break;
		case 2:
			this->actionImg->BackgroundImage = res->end;
			b=new System::Drawing::Size(30,30);
			this->label->Text = gcnew System::String("End");
			this->label->Visible = false;
			this->actionImg->Size = *b;
			delete b;
			break;

		case 3:
			this->actionImg->BackgroundImage = res->signal;
			b=new System::Drawing::Size(150,75);
			this->label->Text = gcnew System::String("SignalRx");
			this->label->Visible = true;
			this->actionImg->Size = *b;
			delete b;
			break;

		case 4:
			this->actionImg->BackgroundImage = res->signal_emm;
			b=new System::Drawing::Size(30,50);
			this->label->Text = gcnew System::String("Signal");
			this->label->Visible = true;
			this->actionImg->Size = *b;
			delete b;
			break;

		case 5:
			this->actionImg->BackgroundImage = res->link;
			b=new System::Drawing::Size(8,120);
			this->label->Text = gcnew System::String(".");
			this->label->Visible = false;
			this->actionImg->Size = *b;
			delete b;
			break;

		case 6:
			this->actionImg->BackgroundImage = res->link;
			b=new System::Drawing::Size(120,8);
			this->label->Text = gcnew System::String(".");
			this->label->Visible = false;
			this->actionImg->Size = *b;
			delete b;
			break;

		case 7:
			this->actionImg->BackgroundImage = res->link;
			b=new System::Drawing::Size(7,30);
			this->label->Text = gcnew System::String("Comment");
			this->label->Visible = true;
			this->actionImg->Size = *b;
			delete b;
			break;

		default:
			this->actionImg->BackgroundImage = res->action;
			b=new System::Drawing::Size(150,75);
			this->label->Text = gcnew System::String("Action");
			this->label->Visible = true;
			this->actionImg->Size = *b;
			delete b;
			break;
	 }
}
int Action::get(char parameter)
{
	switch( parameter )
	{   
		case 'x': 
		case 'X':  
			return (int)this->actionImg->Location.X;
			break;

		case 'y': 
		case 'Y':  
			return (int)this->actionImg->Location.Y;
			break;

		case 'w': 
		case 'W':  
			return (int)this->actionImg->Size.Width;
			break;

		case 'h': 
		case 'H':  
			return (int)this->actionImg->Size.Height;
			break;

		case 'l': 
		case 'L':  
			return (int)this->label->Text->Length;
			break;

		case 't': 
		case 'T':  
			return this->type;
			break;   

		default:
			break;
	}
}
void Action::move(int x,int y)
{
	if (!(0<x))
	{
		x = 0;
	}
	if (!(x<(722 - this->actionImg->Size.Width)))
	{
		x = 722 - this->actionImg->Size.Width;
	}
	if (!(0<y))
	{
		y = 0;
	}
	if (!(y<(513 - this->actionImg->Size.Height)))
	{
		y = 513 - this->actionImg->Size.Height;
	}
	this->actionImg->Location = System::Drawing::Point(x,y);
}
void Action::resize(int x,int y,int w,int h)
{
	if (!(100<w))
	{
		w = 100;
	}
	if (!(w<200))
	{
		w = 200;
	}
	if (!(50<h))
	{
		h = 50;
	}
	if (!(h<100))
	{
		h = 100;
	}
	if (!(0<x))
	{
		x = 0;
	}
	if (!(x<(722-w)))
	{
		x = 722-w;
	}
	if (!(0<y))
	{
		y = 0;
	}
	if (!(y<(513-h)))
	{
		y = 513-h;
	}
	if (!((this->type == -1) || (this->type == 1) || (this->type == 2) || (this->type == 4) || (this->type == 5) || (this->type == 6) || (this->type == 7)))
	{
		this->actionImg->Location = System::Drawing::Point(x,y);
		this->actionImg->Size = System::Drawing::Size(w,h);
	}
}
System::Windows::Forms::PictureBox^  Action::getActionImg()
{
	return this->actionImg;
}
System::Windows::Forms::Label^  Action::getLabel()
{
	return this->label;
}
void Action::setText(System::String^ newText)
{
	this->label->Text = newText;
	this->actionImg->Focus();
}